package com.resurvolution.kingcloudmusicspringboot.product.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.resurvolution.kingcloudmusicspringboot.common.utils.BaseContext;
import com.resurvolution.kingcloudmusicspringboot.product.entity.FavoriteEntity;
import com.resurvolution.kingcloudmusicspringboot.product.entity.SongsEntity;
import com.resurvolution.kingcloudmusicspringboot.product.mapper.FavoriteMapper;
import com.resurvolution.kingcloudmusicspringboot.product.service.FavoriteService;
import com.resurvolution.kingcloudmusicspringboot.product.service.SongsService;
import com.resurvolution.kingcloudmusicspringboot.product.vo.PageVO;
import com.resurvolution.kingcloudmusicspringboot.product.vo.SongsPageVO;
import com.resurvolution.kingcloudmusicspringboot.product.vo.SongsVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, FavoriteEntity> implements FavoriteService {
    @Resource
    FavoriteMapper favoriteMapper;

    @Resource
    SongsService songsService;

    @Override
    public boolean insertLoveSong(Integer id) {
        QueryWrapper<FavoriteEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("song_id", id);
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        FavoriteEntity favoriteEntity = favoriteMapper.selectOne(queryWrapper);
        if (favoriteEntity == null) {
            favoriteEntity = new FavoriteEntity();
            favoriteEntity.setSongId(id);
            favoriteEntity.setUserId(BaseContext.getCurrentId());
            favoriteEntity.setCreatedAt(LocalDateTime.now());
            favoriteMapper.insert(favoriteEntity);
            return true;
        } else {
            favoriteMapper.deleteById(favoriteEntity.getId());
            return false;
        }
    }

    @Override
    public boolean selectSongBySongId(Integer id) {
        QueryWrapper<FavoriteEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("song_id", id);
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        return favoriteMapper.selectOne(queryWrapper) == null;
    }

    @Override
    public PageVO<SongsPageVO> getFavoriteSongs(Integer page, Integer size, String sortBy, String sortOrder, Integer userId) {
        QueryWrapper<FavoriteEntity> queryWrapperFE = new QueryWrapper<>();
        queryWrapperFE.eq("user_id", userId);
        if ("asc".equalsIgnoreCase(sortOrder)) {
            queryWrapperFE.orderByAsc(sortBy);
        } else if ("desc".equalsIgnoreCase(sortOrder)) {
            queryWrapperFE.orderByDesc(sortBy);
        } else {
            queryWrapperFE.orderByAsc("favorite_id");
        }

        PageVO<SongsPageVO> pageVOFE = new PageVO<>();
        pageVOFE.setRecords(favoriteMapper.selectPage(new Page<>(page, size), queryWrapperFE)
                .getRecords()
                .stream()
                .map(favoriteEntity -> {
                    SongsVO songsVO = songsService.getSongById(favoriteEntity.getSongId());
                    SongsPageVO songsPageVO = new SongsPageVO();
                    songsPageVO.setId(songsVO.getId());
                    songsPageVO.setName(songsVO.getName());
                    songsPageVO.setImgUrl(songsVO.getTinyImg());
                    songsPageVO.setArtist(songsVO.getArtist());
                    songsPageVO.setGenre(songsVO.getGenre());
                    return songsPageVO;
                }).toList());
        pageVOFE.setTotal(favoriteMapper.selectCount(queryWrapperFE));
        return pageVOFE;
    }
}
